package com.dhwl.hsh.merchant.presenter;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.dhwl.hsh.lib.bean.HttpResponse;
import com.dhwl.hsh.lib.bean.resp.CarTypeResp;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.BasePresenter;
import com.dhwl.hsh.lib.core.mvp.MvpView;
import com.dhwl.hsh.lib.utils.ReservoirUtils;
import com.dhwl.hsh.lib.utils.ToastUtils;
import com.dhwl.hsh.merchant.HshApplication;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.model.impl.CommonModel;
import com.dhwl.hsh.merchant.ui.activity.BaseActivity;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by hjd on 2016/5/26.
 */
public class TruckPresenter extends BasePresenter<MvpView> {

    private ReservoirUtils reservoirUtils;

    public TruckPresenter() {
        this.reservoirUtils = new ReservoirUtils();
    }


    public void getTruck() {
        Subscriber<HttpResponse<List<CarTypeResp>>> subscriber = new Subscriber<HttpResponse<List<CarTypeResp>>>() {
            @Override
            public void onCompleted() {
                if (TruckPresenter.this.mCompositeSubscription != null)
                    TruckPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("getTruck", e.getMessage());
                ((BaseActivity) TruckPresenter.this.getMvpView()).hideWaitingDialog();
                ToastUtils.show(HshApplication.mContext, R.string.network_not_exist, Toast.LENGTH_SHORT);
                HshException exception = new HshException(e.hashCode(), e.getMessage());
                TruckPresenter.this.getMvpView().onFailure(exception);
            }

            @Override
            public void onNext(HttpResponse<List<CarTypeResp>> response) {

                if (response.getStatus() != 0) {
                    ToastUtils.show((Context) getMvpView(), response.getDescribe(), Toast.LENGTH_LONG);
                    HshException exception = new HshException(response.getStatus(), response.getDescribe());
                    TruckPresenter.this.getMvpView().onFailure(exception);
                } else {

                    List<CarTypeResp> carTypeResp = response.getData();
                    reservoirUtils.refresh("trucks", carTypeResp);
                    TruckPresenter.this.getMvpView().onSuccess(carTypeResp);

                }
            }
        };

        this.mCompositeSubscription.add(CommonModel.getInstance().getCarType().subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) TruckPresenter.this.getMvpView()).showWaitingDialog();
            }
        }).doAfterTerminate(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) TruckPresenter.this.getMvpView()).hideWaitingDialog();
            }
        }).subscribe(subscriber));
    }
}